package com.meihua.shellapp.activity.webview;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.webkit.JavascriptInterface;
import android.webkit.WebView;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.lzy.okgo.OkGo;
import com.meihua.shellapp.R;
import com.meihua.shellapp.activity.OrderPayActivity;
import com.meihua.shellapp.base.BaseBean;
import com.meihua.shellapp.base.BaseWebViewActivity;
import com.meihua.shellapp.base.MyApplication;
import com.meihua.shellapp.utils.LogUtils;
import com.meihua.shellapp.utils.MyStringCallBack;
import com.meihua.shellapp.utils.ToastUtils;
import com.meihua.shellapp.utils.URL;

import butterknife.Bind;

/**
 * 只显示webview的通用页面
 * <p>
 * 作者：lc on 2017/10/19.
 * 邮箱：lc0607live@163.com
 */

public class WebActivity extends BaseWebViewActivity {

    @Bind(R.id.tv_title)
    TextView tvTitle;//标题
    @Bind(R.id.iv_top_right)
    ImageView iv_top_right;//关注
    @Bind(R.id.common_problem_webview)
    WebView webview;//web页面
    @Bind(R.id.iv_top_left)
    ImageView mIVTopLeft;//返回键
    @Bind(R.id.loading)
    ProgressBar mLoading;//加载进度

    private String mGid;//产品id
    private String isFollow = "0";//是否关注  0,没关注   1，关注

    @Override
    public int getLayoutId() {
        return R.layout.activity_home_detail;
    }

    @Override
    public void initView(Bundle savedInstanceState) {
        String url = getIntent().getStringExtra("url");
        iv_top_right.setImageResource(R.drawable.attention);
        tvTitle.setVisibility(View.VISIBLE);
        mIVTopLeft.setVisibility(View.VISIBLE);
        initWebView(webview, mLoading, url,tvTitle);
        webview.addJavascriptInterface(new JS(), "android");
        iv_top_right.setOnClickListener(this);
        mIVTopLeft.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        super.onClick(view);
        switch (view.getId()) {
            case R.id.iv_top_right://关注
                if (isFollow.equals("0")) {
                    addFollow();
                } else if (isFollow.equals("1")) {
                    delFollow();
                }
                break;
        }
    }

    @Override
    public void finish() {
        if (null == mWebView) return;
        if (mWebView.canGoBack()) {
            mWebView.goBack();
            return;
        }
        super.finish();
    }

    /**
     * 添加关注
     */
    private void addFollow() {
        OkGo.<String>post(URL.getUrl().ADDFOLLOW)
                .params("mid", MyApplication.getInstance().getMId())
                .params("product_id", mGid)
                .execute(new MyStringCallBack<BaseBean>(iv_top_right) {
                    @Override
                    public void onSuccessData(BaseBean bean) {
                        super.onSuccessData(bean);
                        if (bean.getCode() == 1) {
                            isFollow = "1";
                            iv_top_right.setImageResource(R.drawable.attentioned);
                            //改变图标
                        }
                        ToastUtils.getInstance().showToast(bean.getMsg());
                    }
                });
    }

    /**
     * 删除关注
     */
    private void delFollow() {
        OkGo.<String>post(URL.getUrl().DELFOLLOW)
                .params("mid", MyApplication.getInstance().getMId())
                .params("gid", mGid)
                .execute(new MyStringCallBack<BaseBean>(iv_top_right) {
                    @Override
                    public void onSuccessData(BaseBean bean) {
                        super.onSuccessData(bean);
                        if (bean.getCode() == 1) {
                            isFollow = "0";
                            iv_top_right.setImageResource(R.drawable.attention);
                        }
                        ToastUtils.getInstance().showToast(bean.getMsg());
                    }
                });
    }

    class JS {
        /**
         * 支付
         */
        @JavascriptInterface
        public void toPay(){
            startActivity(new Intent(WebActivity.this, OrderPayActivity.class));
        }

        /**
         * 设置标题
         *
         * @param json
         */
        @JavascriptInterface
        public void setTitle(final String json) {
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
//                    tvTitle.setText(json);
                }
            });
        }

        /**
         * 设置是否显示关注
         *
         * @param id
         * @param isShow
         */
        @JavascriptInterface
        public void setFollow(final String id, final boolean isShow, final String isFollowtmp) {
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    mGid = id;
                    isFollow = isFollowtmp;
                    LogUtils.getInstance().d(isFollowtmp);
                    if (isShow) {
                        iv_top_right.setVisibility(View.VISIBLE);
                        if (isFollow.equals("0")) {
                            iv_top_right.setImageResource(R.drawable.attention);
                        } else if (isFollow.equals("1")) {
                            iv_top_right.setImageResource(R.drawable.attentioned);
                        }
                    } else {
                        iv_top_right.setVisibility(View.GONE);
                    }
                }
            });
        }
    }

}
